import React from 'react'
// =====================================================================
import Encrypt from '@com/plugins/encrypt'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import Controls from '#cpt/temp/Controls'
import FormModal from '#cpt/temp/FormModal'
// ===================================================================== global declare
const { $fn, $http, $Lazy } = window
// ===================================================================== $Lazy
const Page 		= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 	= $Lazy.file(()=>import('#private/cache-api'))
const Tips 		= $Lazy.load(()=>import('#tp/tips'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		selectedRows:[],
		// 搜索表单列表
		forms : [
			{ label: '科室',	name: 'team_id',	type:'select',	loading:true,	idStr:'uuid' },
			{ label: '标本架',	name: 'shelf_id',	type:'select',	loading:true,	idStr:'uuid' },
		],
		formData : [
			{ label: '科室',		name: 'team_id',	type:'select',		full:true,	required:true, 	loading:true,	idStr:'uuid' },
			{ label: '姓名', 		name: 'name', 		p:' ',				full:true,	disabled:true },
			{ label: '用户名',		name: 'account',						full:true,	required:true,	onBlur:(value)=>{this.getName(value)} },
			{ label: '密码',		name: 'password',	type:'password',	full:true,	required:true,	onBlur:(value)=>{this.getName(value)} },
		],
	}
	submit = {}
	
	componentDidMount(){
		this.getSelect()
		
		this.fetch()
	}
	// 获取下拉数据
	getSelect = () => {
		const { forms, formData } = this.state
		
		$cache.then( async f => {
			// 科室
			const _data = await f.getCache({ api:'ke_shi' })
			$fn.setRow( forms, 'team_id', 	_data )
			$fn.setRow( formData, 'team_id', 	_data )
			// 标本架
			const shelfData = await f.getCache({ api:'shelf/quickSelect' })
			shelfData.forEach(v => {v.name = v.name + '-' + v.sf_code + '-' + v.sf_number})
			$fn.setRow( forms, 'shelf_id', shelfData )
			
			this.setState({ forms, formData })
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'receive/quickPullList')
	// 根据账号获取姓名
	getName = (value)=>{
		if(value){
			const formValue = this.formRef.getValue(['account','password'])
			$http.submit(null, 'employee/getuserrealname', {param:{phone:formValue.account,password:formValue.password},loading:false }).then((data)=> {
				if(!$fn.hasArray(data)){
					if(data.length === 0){
						this.formRef.setValue({name:''})
					}else{
						this.formRef.setValue({name:data.real_name})
					}
				}
			})
		}
	}
	
	// 表单
	cols = [
		{ title: '标本条码', 	dataIndex: 'spec_code', 	...$fn.table.spec_code },
		{ title: '标本架编号', 	dataIndex: 'sf_number', 	...$fn.table.exp_num },
		{ title: '实验号', 		dataIndex: 'lb_tpl', 		...$fn.table.exp_num },
		{ title: '创建人', 		dataIndex: 'founder', 		...$fn.table.name },
		{ title: '创建日期', 	dataIndex: 'created_at', 	...$fn.table.time },
		{ title: '领取人', 		dataIndex: 'receive_user',  ...$fn.table.name},
		{ title: '领取日期', 	dataIndex: 'receive_time',  ...$fn.table.time},
		{ title: '岗位', 		dataIndex: 'job_id', 		width:100 },
		{ title: '科室', 		dataIndex: 'team_name', 	width:100 },
	]
	
	// 操作按钮
	controls = () => {
		return [
			Controls.back.call(this ), 	// 返回
			Controls.click.call(this, '领取', () => { 	// 领取
				const userData = $fn.getUser()
				const { empl_name, real_name } = userData
				const m = $fn.longSave('remember')
				if($fn.hasObject(m)){
					for(var i in m){ m[i] = Encrypt.decode(m[i]) }
				}
				this.formRef.setValue({account: empl_name, password: m?m.password:'', name:real_name })
				this.modal.open({formRef: this.formRef})
				this.submit.uuid = this.state.selectedKeys
			}, { isKeys:1, code:'F3' })
		]
	}
		
	render(){
		return (
			<>
				<Page title='快捷领取'  controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this) }
				</Page>
				{FormModal.call(this, {title:'领取', labelWidth:'s4', width:700, api:'receive/quickAdd',
					onRefresh: ()=>{
						this.modal.close()
						this.setState({
							selectedKeys:[],
							selectedRows:[]
						})
					},
					prefix: (<Tips text='请输入用户名与密码进行领取确认' className = 'mb5' />)
				})}
			</>
		)
	}
}

export default Index